package com.lxb.myforum.repository

import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.stereotype.Repository
import com.lxb.myforum.pojo.Role

/**
 *
 * @author lxb
 */
@Repository
interface RoleRepository : PagingAndSortingRepository<Role, Long>, JpaSpecificationExecutor<Role> {
    /**
     * 获取id 为*userId*的用户没有的角色
     * @param userId 要查询的用户id
     * @return
     */
    @Query(value = "select a.* from `t_role` a left join `t_user_role` b on a.id = b.role_id where b.`user_id` != ?1 || ISNULL(b.user_id)", nativeQuery = true)
    fun findNotUserRoles(userId: Long): List<Role>

    @Query(value = "select a.* from `t_role` a left join `t_user_role` b on a.id = b.role_id where b.`user_id` = ?1", nativeQuery = true)
    fun findOwnRoles(userId: Long): List<Role>
}
